<?php
	// Class: CalendarUI
	// 
	// Class to draw calendar user interface.
	//
	// Author: Arttu Valo
	// Date: 22.11.2006
	// Kaikki virheilmoitukset ja varoitukset esille
	error_reporting(E_ALL);
	
	require_once("../TableGenerator/Table.php");
	require_once("../TableGenerator/Tr.php");
	require_once("../TableGenerator/Td.php");
	require_once("../TableGenerator/Th.php");
	require_once("Calendar.php");
	// Include version information
	//require_once("common/version.inc.php");

	
	// GetVersion
	// Returns version information
	// 
	// Retuns a string containing the version
	function GetVersion()
	{
		return VERSION." b".BETA_VERSION;
	}

	
	// calendar start hour and time step in minutes
	$nStartHour = 9;
	$nEndHour = 22;
	$nStep = 30;
	
	// RenderDayView
	//
	// Params:
	//	$date - time stamp for the day	
	function RenderDayView($date)
	{
		global $nStartHour;
		global $nEndHour;
		global $nStep;
		
		$table = new Table();
		$table->setBorder(1);
		$table->setWidth("25%");
		
		// date row
		$row = new Tr();
		$header = new Th(date("Y-m-d", $date));
		$header->setColSpan(2);
		$row->addColumn($header);
		$table->setColumnHeaders($row);
		
		// header row
		$row = new Tr();
		$row->addColumn(new Th("Klo"));
		$dateinfo = getdate($date);
		$row->addColumn(new Th($dateinfo["weekday"]));
		$table->addRow($row);
		
		$cal = new Calendar(DBConnector::DBC_MSSQL, "AEVALO", "opennh", "opennh", "OpenNeighbourhood", "Events");
		
		$events = $cal->GetEventsForDay($date);
		
		$now = getdate($date);
		$now["hours"] = $nStartHour;
		$now["minutes"] = 0;
		$now["seconds"] = 0;
		
		while($now["hours"] < $nEndHour)
		{
			// contents
			$row = new Tr();
			$row->addRowheader(new Th($now["hours"].":".$now["minutes"]));
			$contents = new Th("&nbsp;");
			foreach($events as $event)
			{
				$start = getdate(strtotime($event->EventStart));
				$end = getdate(strtotime($event->EventEnd));
				if($now["hours"] == $start["hours"] && $now["minutes"] == $start["minutes"])
				{
					// Calculate the rowspan
					$sSpan = mktime($start["hours"] - $nStartHour, $start["minutes"], 0);
					$eSpan = mktime($end["hours"] - $nStartHour, $end["minutes"], 0);
					$timeSpan = getdate($eSpan - $sSpan);
					
					$hours = $end["hours"] - $start["hours"];
					$minutes = $end["minutes"] - $start["minutes"];
					
					$rowSpan = ($hours * 60 + $minutes)/$nStep;
					$contents->setRowSpan($rowSpan);
					
					// generate contents					
					$deleteBtn = sprintf("<button type=\"submit\" name=\"delete\" value=\"%d\">Delete</button>",
						$event->EventID);
					//$editBtn = sprintf("<button type=\"submit\" name=\"edit\" value=\"%d\">Edit</button>",
					//	$event->EventID);
					
					$contentStr =	"<div style='float:left'>".
									"<span style='font-weight:bold;'>".$event->EventHeader."</span><br>".
									$event->EventText."</div><div style='float:left'>".$deleteBtn."</div>";
					//				$editBtn."<br/>\n".$deleteBtn."</div>";
					
					$contents->setContent($contentStr);
					
					// select text color					
					switch($event->EventType)
					{
						case 1:
							$contents->setStyle("color: red;");
							break;
						case 2:
							$contents->setStyle("color: green;");
							break;
						case 3:
							$contents->setStyle("color: blue;");
							break;
					}
				}
				// hide unwanted cells
				if($now[0] > $start[0] && $now[0] < $end[0])
				{
					$contents->setStyle("visibility: hidden");
				}
			}
			
			// insert unhidden cells			
			if(strpos($contents->getStyle(), "hidden") == FALSE)
			{
				$row->addColumn($contents);
			}
			
			$table->addRow($row);
			
			$now["minutes"] = $now["minutes"] + $nStep;
			$now = getdate(mktime($now["hours"],$now["minutes"],0,$now["mon"],$now["mday"],$now["year"]));
		}
		
		echo $table->getHtml();
	}
	
	// RenderMonthView
	//
	// Params:
	//	$date - time stamp for the month
	function RenderMonthView($date)
	{
		$cal = new Calendar(DBConnector::DBC_MSSQL, "AEVALO", "opennh", "opennh", "OpenNeighbourhood", "Events");
		/* Set some variables */
		$dayCount=01;
		$day=01;
		$off=0;
		
		/* The month and year variables can (should) be passed from a preceding
		web page rather than hard coded */
		
		$now = getdate($date);
		$month = $now["mon"];
		$year =  $now["year"];
		
		/* Figure out how many days are in this month */
		while(checkdate($month,$dayCount,$year))
		{
			$dayCount++;
		}
		
		// generate the table		
		$table = new Table();
		$table->setBorder(1);
		$table->setWidth("100%");
		
		// header row		
		$headerRow = new Tr();
		$header = new Th(date("F Y", $date));
		$header->setColSpan(8);
		$headerRow->addColumn($header);
		$table->addRow($headerRow);
		
		// the weekdays		
		$headerRow = new Tr();
		$headerRow->addColumn(new Th("Monday"));
		$headerRow->addColumn(new Th("Tuesday"));
		$headerRow->addColumn(new Th("Wednesday"));
		$headerRow->addColumn(new Th("Thursday"));
		$headerRow->addColumn(new Th("Friday"));
		$headerRow->addColumn(new Th("Saturday"));
		$headerRow->addColumn(new Th("Sunday"));
		
		$table->addRow($headerRow);
		
		// add the cells		
		$row = new Tr();
		while($day < $dayCount)
		{
			$events = $cal->GetEventsForDay(mktime(0,0,0,$month,$day,$year));
			$format = "<button type=\"submit\" name=\"setdate\" value=\"%d\"".
				" style=\"border-style: none; width: 100%%; height: 100%%;background-color: inherit;%s\">%d</button>";
			$font = "";
			// differentiate the days that have events
			if(count($events) > 0)
			{
				$font = "font-weight: bold";
			}

			$cell = new Td(sprintf($format, mktime(0,0,0,$month,$day,$year), $font, $day));
			
			// handle month starting in the middle of the week			
			if ($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Monday') {
				$row->addColumn($cell);
				$off= '01';
			}
			elseif ($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Tuesday') {
				$row->addColumn(new Td(""));
				$row->addColumn($cell);
				$off= '02';
			}
			elseif ($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Wednesday') {
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn($cell);
				$off= '03';
			}
			elseif ($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Thursday') {
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn($cell);
				$off= '04';
			}
			elseif ($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Friday') {
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn($cell);
				$off= '05';
			}
			elseif ($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Saturday') {
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn($cell);
				$off= '06';
			}
			elseif($day == 1 and date('l', mktime(0,0,0,$month,$day,$year)) == 'Sunday')
			{
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn(new Td(""));
				$row->addColumn($cell);
				$off = '07';
			}
			else {
				$row->addColumn($cell);
			}
			
			$day++;
			$off++;
			
			if ($off>7) {
				$table->addRow($row);
				$row = new Tr();
				$off='01';
			} else {
				echo "";
			}
		}
		
		if($row->getColumnCount() > 0)
		{
			$table->addRow($row);
		}
		
		echo $table->getHtml();
	}
	
	// get the variables from the form	
	if(isset($_POST["date"]))
	{
		$date = intval($_POST["date"]);
	}
	else
	{
		$date = time();
	}
	if(isset($_POST["view"]))
	{
		$view = $_POST["view"];
	}
	else
	{
		$view = "month";
	}
	// handle commands
	if(isset($_POST["action"]))
	{
		if($_POST["action"] == "dayView")
		{
			$view = "day";
		}
		elseif($_POST["action"] == "monthView")
		{
			$view = "month";
		}
		elseif($_POST["action"] == "addNew")
		{
			header("Location: NewEvent.php");
			$view = $_POST["view"];
		}
		// move to prev day or month
		elseif($_POST["action"] == "prev")
		{
			switch($view)
			{
				case "day":
					$date = getdate($date);
					$date["mday"] = $date["mday"] - 1;
					break;
				case "month":
					$date = getdate($date);
					$date["mon"] = $date["mon"] - 1;
					break;
			}
			$date = mktime(0,0,0,$date["mon"],$date["mday"],$date["year"]);
			$view = $_POST["view"];
		}
		// move to next month or month
		elseif($_POST["action"] == "next")
		{
			switch($view)
			{
				case "day":
					$date = getdate($date);
					$date["mday"] = $date["mday"] + 1;
					break;
				case "month":
					$date = getdate($date);
					$date["mon"] = $date["mon"] + 1;
					break;
			}
			$date = mktime(0,0,0,$date["mon"],$date["mday"],$date["year"]);
			$view = $_POST["view"];
		}
	}
	// rest of the actions
	if(isset($_POST["delete"]))
	{
		$cal = new Calendar(DBConnector::DBC_MSSQL, "AEVALO", "opennh", "opennh", "OpenNeighbourhood", "Events");
		$cal->DeleteEvent($_POST["delete"]);
	}
	if(isset($_POST["edit"]))
	{
		header("Location: NewEvent.php?editID=".$_POST["edit"]);
		$view = $_POST["view"];
	}
	if(isset($_POST["setdate"]))
	{
		$date = $_POST["setdate"];
		$view = "day";
	}
?>
<html>
	<head>
		<title>Calendar</title>
	</head>
	<body>
		<form action=<?php print("\"".$_SERVER["PHP_SELF"]."\""); ?> method="post">
			<!-- add the variables to the form //-->
			<button type="submit" name="action" value="prev">&lt;&lt;</button>
			<button type="submit" name="action" value="dayView">Day view</button>
			<button type="submit" name="action" value="monthView">Month view</button>
			<button type="submit" name="action" value="next">&gt;&gt;</button><br>
			<button type="submit" name="action" value="addNew">Add new event</button>
			<input type="hidden" name="view" value=<?php print("\"$view\""); ?>/>
			<input type="hidden" name="date" value=<?php print("\"$date\""); ?>/>
			<?php
				// render correct view
				switch($view)
				{
					case "day":
						RenderDayView($date);
						break;
					case "month":
						RenderMonthView($date);
						break;
				}
			?>
		</form>
	</body>
</html>
